/**
 * 
 */
package view;

import java.awt.Dimension;
import java.awt.GridLayout;

import javax.swing.JPanel;

import model.GameDriver;
import model.ManualWeapon;

import common.DataManager;

/**
 * @author Bill
 * 
 * Panel representing the view of the "Cargo" class. Contains several clickable images
 * which can be clicked to select a manual weapon to fire when in attack mode.
 * This panel is only seen in the attack stage.
 *
 */
public class CargoPanel extends JPanel {
	
	private static final int CARGO_PANEL_HEIGHT = 500;
	private static final int CARGO_PANEL_WIDTH = 250;
	
	JPanel grid = new JPanel(null);
	
	/**
	 * Constructs the cargo panel interface
	 */
	public CargoPanel(){
		
		GridLayout cargoPanelLayout = new GridLayout(0,1);
		cargoPanelLayout.setVgap(30);
		grid = new JPanel(cargoPanelLayout);
		grid.setOpaque(false);
		this.setOpaque(false);
		
		for(int j = 0; j < DataManager.getManualWeaponIcons().size(); j++){
			CargoItemPanel itemPanel = new CargoItemPanel(DataManager.getManualWeaponIcons().get(j));
			grid.add(itemPanel);
		}
		grid.setSize(grid.getPreferredSize());
		this.add(grid);
		grid.setLocation((CARGO_PANEL_WIDTH-ItemIconPanel.getIconWidth())/2, (CARGO_PANEL_HEIGHT-grid.getHeight())/2);
		System.out.println(grid.getLocation());
		this.setSize(new Dimension(CARGO_PANEL_WIDTH,CARGO_PANEL_HEIGHT));
		this.setPreferredSize(new Dimension(CARGO_PANEL_WIDTH,CARGO_PANEL_HEIGHT));
	}
	
	/**
	 * unhighlights all of the cargo items
	 */
	public void unhighlightAllItems(){
		for(int i = 0; i < grid.getComponentCount(); i++){
			ItemIconPanel item = (ItemIconPanel)grid.getComponent(i);
			item.unhighlightItem();
		}
	}
	
	/**
	 * finds a particular itemIconPanel from it's image name
	 */
	public CargoItemPanel findCargoItemPanelFromName(String name){
		for(int i = 0; i < grid.getComponentCount(); i++){
			CargoItemPanel item = (CargoItemPanel)grid.getComponent(i);
			if(item.getItemName() == name) return item;
		}
		return null;
	}
	
	/**
	 * Uses information from the model to update the cargo view
	 * 
	 * @param GameDriver gameDriver
	 */
	public void updateCargo(GameDriver gameDriver){
		unhighlightAllItems();
		if(gameDriver.getPlayer().getCargo().isWeaponSelected()){
			System.out.println("Selected weapon is "+gameDriver.getPlayer().getCargo().getSelectedWeapon());
			CargoItemPanel cargoItem = findCargoItemPanelFromName(gameDriver.getPlayer().getCargo().getSelectedWeapon());
			cargoItem.highlightItem();
		}
		for(int i = 0; i < gameDriver.getPlayer().getCargo().getManualWeapons().size(); i++){
			ManualWeapon weapon = gameDriver.getPlayer().getCargo().getManualWeapons().get(i);
			CargoItemPanel itemIcon = findCargoItemPanelFromName(weapon.getImageName());
			itemIcon.setBottomLabel("x"+gameDriver.getPlayer().getCargo().getManualWeapons().get(i).getAmmo().size());
			if(gameDriver.getPlayer().getCargo().getManualWeapons().get(i).getAmmo().size() <= 0)
				itemIcon.coverItem();
			else itemIcon.uncoverItem();
		}
	}

}
